package org.example.likou150.array;

/**
* https://leetcode.cn/problems/product-of-array-except-self/?envType=study-plan-v2&envId=top-interview-150
* */
public class productExceptSelf {

    public static void main(String[] args) {
        int[] nums=new int[]{1,2,3,4};
        productExceptSelf productExceptSelf =new productExceptSelf();
        int[] ints = productExceptSelf.productExceptSelf(nums);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public int[] productExceptSelf(int[] nums) {
        int[] result = new int[nums.length];
        int temp=1;
        for(int i=0; i<nums.length; i++){
            result[i]=temp;
            temp *= nums[i];
        }
        temp=1;
        for(int i=nums.length-1; i>=0; i--){
            result[i] *= temp;
            temp*=nums[i];
        }

        return result;
    }






//    public int[] productExceptSelf(int[] nums) {
//        int[] left = new int[nums.length];
//        left[0]=1;
//        for (int i = 1; i < nums.length; i++) {
//            left[i]=left[i-1]*nums[i-1];
//        }
//        int[] right = new int[nums.length];
//        right[nums.length-1]=1;
//        for(int i=nums.length-2; i>=0; i--){
//            right[i]=right[i+1]*nums[i+1];
//            left[i]=left[i]*right[i];
//        }
//        return left;
//    }
}

/*
* 1 2 3 4
* 1 1 3 4
* 1 2 1 4
* 1 2 3 1
* */
